/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-20 14:56:50
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-27 11:43:11
 * @FilePath: \gogamewebserver\goGameWebServer\internal\controller\account\account_v1_private.go
 * @Description: 文件功能描述
 */
package account

import (
	"context"
	"encoding/json"
	v1 "goGameWebServer/api/account/v1"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/defines/platform"
	"goGameWebServer/internal/manager/platformmanager"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *AccountControllerV1Private) UpdateToken(ctx context.Context, req *v1.AccountUpdateReq) (res *v1.AccountUpdateRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if acD.AvatarId != 0 {
		controller.BackResponseError(r, cerror.AVATAR_ALREADY_EXIST_ERROR, cerror.GetErrMsg(cerror.AVATAR_ALREADY_EXIST_ERROR))
		return
	}
	acD.ThirdToken = req.ThirdToken
	acD.ThirdTokenExpireTime = req.ThirdTokenExpireTime
	ac.SaveEntityDataToDB()
	controller.BackResponseOK(r, nil)
	return
}

func (c *AccountControllerV1Private) CallThird(ctx context.Context, req *v1.AccountCallThirdReq) (res *v1.AccountCallThirdRes, err error) {
	platformOpt := platformmanager.ManagerPlatform().GetPlatform(platform.AliPayGame)
	if platformOpt == nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_TYPE_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_TYPE_ERROR))
		return
	}
	reqDatas := make(map[string]string)
	e := json.Unmarshal([]byte(req.Data), &reqDatas)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.JSON_UNMARSHAL_ERROR, cerror.GetErrMsg(cerror.JSON_UNMARSHAL_ERROR))
		return
	}
	re, e := platformOpt.CallThird(req.Url, reqDatas, nil)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_CALL_THIRD_ERROR, cerror.GetErrMsg(cerror.HTTP_CALL_THIRD_ERROR)+e.Error())
		return
	}
	res = &v1.AccountCallThirdRes{}
	jDatas, e := json.Marshal(re)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.JSON_MARSHAL_ERROR, cerror.GetErrMsg(cerror.JSON_MARSHAL_ERROR))
		return
	}
	res.Data = string(jDatas)
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}
